package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.manager.PinYinSortManager;
import com.hangzhou.xyj.model.DramaCategoryModel;
import com.hangzhou.xyj.model.DramaPageInfoModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.net.handler.GetListHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetListReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.DramaCategoryListAdapter;
import com.hangzhou.xyj.ui.widget.MyLetterListView;
import com.hangzhou.xyj.utils.StringUtil;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by neo.duan on 2016/1/6.
 * 更多剧种
 */
public class DramaCategoryListActivity extends BaseActivity {
    private RecyclerView mRecyclerView;
    private MyLetterListView mLetterLv; //字母索引

    private HashMap<String, Integer> alphaIndexer;//存放存在的汉语拼音首字母和与之对应的列表位置
    private PinYinSortManager mSortManager;
    private List<DramaCategoryModel> mDataList;
    private DramaCategoryListAdapter mAdapter;
    private int mIntentTag;
    private String mTitle;

    @Override
    public void initIntentData(Intent intent) {
        super.initIntentData(intent);
        mIntentTag = intent.getIntExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_DEFAULT.getValue());
        mTitle = intent.getStringExtra(DataKey.TITLE);
    }

    @Override
    public void initTop() {
        if (StringUtil.isNotEmpty(mTitle)) {
            enableTitle(true, mTitle);
        } else {
            enableTitle(true, "更多剧种");
        }
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_more_drama);
    }

    @Override
    public void initViews() {
        mRecyclerView = findView(R.id.rv_more_drama);
        mLetterLv = findView(R.id.lv_more_drama);
        //设置水平布局
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        mRecyclerView.setAdapter(mAdapter = new DramaCategoryListAdapter(mContext, mDataList));
        mAdapter.setIntentTag(mIntentTag);
    }

    @Override
    public void initDatas() {
        getCategoryList();
    }

    private void getCategoryList() {
        GetListReq req = new GetListReq(1, Integer.MAX_VALUE);
        HttpLoader.getInstance().sendRequest(new GetListHandler(req, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model); //不需要关闭Loading对话框，所以注释掉
                DramaPageInfoModel infoModel = (DramaPageInfoModel) model;
                List<DramaCategoryModel> categoryList = infoModel.getDramaCategoryList();
                //对数据排序
                sortData(categoryList);
                mAdapter.update(mDataList);
            }
        }, 0));
    }

    /**
     * 对数据排序
     */
    private void sortData(List<DramaCategoryModel> categoryList) {
        if (categoryList == null) {
            categoryList = new ArrayList<>();
        }
        this.mDataList = categoryList;
        // 字母排序索引管理器
        mSortManager = new PinYinSortManager(this);
        //进行按字母排序
        List<Object> sortDatas = mSortManager.sortDatas(mDataList, "name");
        mDataList.clear();
        for (Object obj : sortDatas) {
            mDataList.add((DramaCategoryModel) obj);
        }
    }

    @Override
    public void initListeners() {
        mLetterLv.setOnTouchingLetterChangedListener(new MyLetterListView.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                alphaIndexer = mSortManager.getAlphaIndexer();
                if (alphaIndexer.get(s) != null) {
                    int position = alphaIndexer.get(s);
                    mRecyclerView.smoothScrollToPosition(position);
                    mSortManager.handlerView(position);
                }
            }
        });
    }
}
